<script>
tpl = '';
$(function(){
	
	$('input[type=button][rel=submit]').click(function(){
		if(confirm('将提交全天日报，一旦提交将不可变更、追加，请确认！')) 
		 {
			$('#opt').val('submit');
			$("#daily_post").submit();
		 }
	});
	
	$('input[type=button][rel=save]').click(function(){
			$('#opt').val('save');
			$("#daily_post").submit();
	});


    $('#daily_post').submit(function(){
		 var inputs = $('input[type=button]');
		 inputs.attr('disabled','disabled');
		 
			$.post(location.href,$(this).serialize(),function(data){
				alert(data.message);
				inputs.removeAttr('disabled');
				if (data.state == 1)
				{
					window.location.reload();
				}
			},'json');
			return false;
    });

	$('input[type=button][rel=addForms]').click(function(){
		tpl = $('#tpl').clone(1);
		tpl.attr('id',Math.random());
		var tobject = tpl;
		$(tobject).appendTo('#continer');
	});

	$('input[rel=delete]').live('click',function(){
		if ($('table[rel=tables]').length == 1)
		{
			alert('只剩一个了，不能删除');
		}
		else
		{
			var od = $(this).parent().parent().parent().parent();
			od.remove();
		}
		return false;
	});

	$('input[rel=add_item]').live('click',function(){
		var tv = $(this).prev('select').val();
		var tt = $(this).prev('select').find('option:selected').text();
		var tn = $(this).prev('select').val();
		if (tv == '')
		{
			alert('请先选择项目');
			return false;
		}
		else
		{
			if($(this).parent().parent().parent().find('#'+tv).length)
			{  //报销项目不能重复添加
				alert('该项目已添加');
				return false;
			}
			$('<tr><td class="td1">'+tt+'明细：</td><td><textarea id="'+ tv +'" name="'+tn+'_mark[]"></textarea></td><td class="td1">'+tt+'小计：</td><td><input type="text" name="'+tn+'_subtotal[]" onkeyup="checkNum(this)" value="0.00"/><input type="button" rel="deleteItem" value="删除"/></td></tr>').appendTo($(this).parent().parent().parent());
		}
	});
	
	
	$('input[rel=deleteItem]').live('click',function(){
		var od = $(this).parent().parent();
		od.remove();
	});

	
	// work object autocomplete
	$('input[rel=wo]').autocomplete('?app=<?=APP?>&mod=<?=MOD?>&opt=GetCustomer',{
		multiple:false,
		width:150,
		multipleSepartator:'',
		dataType:'json',
		minChars:0,
		parse: function(data) {
			var rows = [];
			for (var i=0; i<data.length ;i++ )
			{
				rows[rows.length] = {
					data: data[i].customer_name,
					value: data[i].customer_name,
					result:data[i].customer_name
				};
			}
			return rows;
		},
		formatItem: function(row,i,n) {
			return row;
		},
		formatResult: function(row,i,n) {
			return row;
		},
		formatMatch: function(row,i,max) {
			return row;
		}
	});
});


function checkNum(obj)
{
 var re = /^-?[1-9]*(\.\d*)?$|^-?d^(\.\d*)?$/;
     if (!re.test(obj.value))
    {
			if(isNaN(obj.value)){  alert("非法数字");
		obj.value="";
        obj.focus();
        return false;}

     }
}  
</script>
<div class="t_2">
     <div class="fl">信息录入</div>
     <div class="fr add_form"></div>
</div>
<div class="order_show" id="info_form">
	 <form id="daily_post" method="post">
	 <div id="continer">
     <?php if($this->act=='add'): ?>
     <table width="100%" cellpadding="0" cellspacing="0" border="0" class="info_table" id='tpl' rel="tables">
            <tr>
                <td class="td1" width="15%">项目阶段：</td>
                <td width="35%">					
					<?=$this->project_phase?>
				</td>
                <td class="td1" width="15%">工作结果：</td>
                <td width="35%">
					<?=$this->work_result?>
				</td>
            </tr>
			<tr>
				<td class="td1">工作说明：</td>
				<td><textarea name="work_mark[]"></textarea></td>
				<td class="td1">工作对象：</td>
				<td><input type="text" name="work_object[]" rel="wo" /></td>
			</tr>
			<tbody>
            <tr>
                <td class="td1">结果说明：</td>
                <td><textarea name="result_mark[]" id="result_mark"></textarea></td>
				<td class="td1">添加项目：</td>
				<td>
					<?=$this->daily_item?>
					<input type="button" rel="add_item" value="添加"/>
				</td>
            </tr>
			</tbody>
            <tr>
                <td class="td1">其他明细：</td>
                <td><textarea name="other_mark[]" id="other_mark"></textarea></td>
                <td class="td1">其他小计：</td>
                <td><input name="other_subtotal[]" id="other_subtotal" onkeyup="checkNum(this)"  value="0.00"/></td>
            </tr>
            <tr>
                <td class="td1">备注：</td>
                <td colspan="2"><textarea name="mark[]" id="mark"></textarea></td>
				<td align="right"><input type="button" rel="delete" value="删除"/></td>
            </tr>
            <tr>
                <td colspan="4">
				</td>
            </tr>
     </table>
     <?php else:?>
		<?php foreach((array)$this->daily_desc as $daily): ?> 
        <table width="100%" cellpadding="0" cellspacing="0" border="0" class="info_table" id='tpl' rel="tables">
            <tr>
                <td class="td1" width="15%">项目阶段：</td>
                <td width="35%">					
					<?=$daily['project_phase']?>
				</td>
                <td class="td1" width="15%">工作结果：</td>
                <td width="35%">
					<?=$daily['work_result']?>
				</td>
            </tr>
			<tr>
				<td class="td1">工作说明：</td>
				<td><textarea name="work_mark[]"><?=$daily['work_mark']?></textarea></td>
				<td class="td1">工作对象：</td>
				<td><input type="text" name="work_object[]" rel="wo" value="<?=$daily['work_object']?>" /></td>
			</tr>
			<tbody>
            <tr>
                <td class="td1">结果说明：</td>
                <td><textarea name="result_mark[]" id="result_mark"><?=$daily['result_mark']?></textarea></td>
				<td class="td1">添加项目：</td>
				<td>
					<?=$this->daily_item?>
					<input type="button" rel="add_item" value="添加"/>
				</td>
            </tr>
            	<?php foreach((array)$daily['itmes'] as $kkey=>$kkvalue): ?> 
                	<tr>
                    	<td class="td1"><?=$kkvalue['title']?>明细：</td>
                        <td>
                        	<textarea id="<?=$kkey?>" name="<?=$kkey?>_mark[]"><?=$kkvalue['mark']?></textarea>
                        </td>
                     	<td class="td1"><?=$kkvalue['title']?>小计</td>
                        <td>
                        	<input type="text" name="<?=$kkey?>_subtotal[]" onkeyup="checkNum(this)" value="<?=$kkvalue['subtotal']?>"/>
                            
                            <input type="button" rel="deleteItem" value="删除"/>
                        </td>
                    </tr>
            	<?php endforeach; ?>
			</tbody>
            
            
            <tr>
                <td class="td1">其他明细：</td>
                <td><textarea name="other_mark[]" id="other_mark"><?=$daily['other_mark']?></textarea></td>
                <td class="td1">其他小计：</td>
                <td><input name="other_subtotal[]" id="other_subtotal" onkeyup="checkNum(this)"  value="<?=$daily['other_subtotal']?>"/></td>
            </tr>
            <tr>
                <td class="td1">备注：</td>
                <td colspan="2"><textarea name="mark[]" id="mark"><?=$daily['mark']?></textarea></td>
				<td align="right"><input type="button" rel="delete" value="删除"/></td>
            </tr>
            <tr>
                <td colspan="4">
				</td>
            </tr>
     </table>
       <?php endforeach; ?>
	<?php endif; ?>
	 </div>
     <table width="100%" cellpadding="0" cellspacing="0" border="0">
            <tr>
                <td class="td1" width="15%">下级操作员列表：</td>
                <td>
					<?php if($this->users): ?>
						<?=$this->users?>
					<?php else: ?>
						无
					<?php endif; ?>
				</td>
            </tr>
            <tr>
                <th colspan="2">
					<input type="button" rel="submit" value="提交全天日报" />
                    <input type="button" rel="addForms" value="添加表单" />
					<input type="button" rel="save" value="保存" />
					<input type="hidden" name="opt" id="opt" value="" />
				</th>
            </tr>
     </table>
	 </form>
</div>